package com.xavier.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xavier.service.IRoleMenuService;
import com.xavier.vo.QueryResultVO;
import com.xavier.utils.BeanUtil;
import com.xavier.utils.ServiceException;
import com.xavier.result.ResultCode;
import com.xavier.convertor.RoleConvertor;
import com.xavier.dto.RoleQueryDTO;
import com.xavier.model.Role;
import com.xavier.dto.RoleDTO;
import com.xavier.vo.RolePageVO;
import com.xavier.vo.RoleDetailVO;
import com.xavier.dao.RoleMapper;
import com.xavier.service.IRoleService;
import com.xavier.utils.XavierLambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.List;


/**
 * 业务逻辑层-角色实现类
 *
 * @author Xavier
 * @version 1.0
 */
@Slf4j
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Resource
    private IRoleMenuService roleMenuService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteRole(Long id) {
        getBaseRole(id);
        return super.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createRole(RoleDTO dto) {
        Role role = RoleConvertor.toRole(dto);
        role.setId(null);
        super.save(role);
        roleMenuService.createRoleMenu(role.getId(), dto.getMenuIds());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateRole(RoleDTO dto) {
        getBaseRole(dto.getId());
        Role role = RoleConvertor.toRole(dto);
        super.updateById(role);
        roleMenuService.updateRoleMenu(role.getId(), dto.getMenuIds());
    }

    @Override
    public RoleDetailVO getRole(Long id) {
        RoleDetailVO vo = RoleConvertor.toRoleDetailVO(getBaseRole(id));
        vo.setMenuIds(roleMenuService.getRoleMenu(id));
        return vo;
    }


    @Override
    public Role getBaseRole(Long id) {
        Role role = super.getById(id);
        if (Objects.isNull(role) || role.getIsDeleted()) {
            throw new ServiceException(ResultCode.COMMON_MESSAGE, "角色不存在");
        }
        return role;
    }

    @Override
    public QueryResultVO<RolePageVO> queryRole(RoleQueryDTO queryDTO) {
        Page<Role> page = new Page<>();
        page.setCurrent(queryDTO.getPageNo());
        page.setSize(queryDTO.getPageSize());
        XavierLambdaQueryWrapper<Role> wrapper = Role.gw();
        wrapper.eqIfPresent(Role::getId, queryDTO.getId());
        wrapper.likeIfPresent(Role::getRoleName, queryDTO.getRoleName());
        wrapper.eqIfPresent(Role::getIsEnable, queryDTO.getIsEnable());
        wrapper.likeIfPresent(Role::getRemarks, queryDTO.getRemarks());
        wrapper.geIfPresent(Role::getCreateTime, queryDTO.getCreateTimeFrom());
        wrapper.leIfPresent(Role::getCreateTime, queryDTO.getCreateTimeTo());
        wrapper.orderByAsc(Role::getId);
        Page<Role> result = super.page(page, wrapper);
        QueryResultVO<RolePageVO> queryResultVO = BeanUtil.pageToQueryResultVO(result, RolePageVO.class);
        queryResultVO.setRecords(RoleConvertor.toRolePageVOList(result.getRecords()));
        return queryResultVO;
    }

}
